home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / cgazv4n2.zip / CPP.ZIP / LDIR.CXX < prev    next >
C/C++ Source or Header  |  1989-08-26  |  2KB  |  56 lines

  1. // LDIR.CXX
  2. #include "filelist.hxx"
  3.  
  4. // The following strings contain no punctuation between them.
  5. // An ANSI C preprocessor will concatenate all these strings
  6. // into a single string.  This is convenient because it allows
  7. // easy creation of a large block of display text, and easy
  8. // modification of that block:
  9. char * errmsg =
  10. "ldir in C++ by Bruce Eckel.  A Unix-like file listing program.\n"
  11. "Usage: ldir [filespec] [flags]\n"
  12. "optional filespec must be second argument, but flags can be\n"
  13. "in any order.  Available options are:\n"
  14. "   -r    recursively expands subdirectories\n"
  15. "   -s    displays sizes\n"
  16. "   -t    displays times\n"
  17. "   -d    displays dates\n"
  18. "   -a    displays attributes\n";
  19.  
  20. main(int argc, char ** argv) {
  21.   display_format format = 0;
  22.   char * arg = "*.*";  // default file spec to all files
  23.  
  24.   // Here's a nice, expandable way to process command-line
  25.   // switches:
  26.   if (argc > 1 && argv[1][0] != '-')
  27.     arg = argv[1];
  28.   for(int a = argc -1;  a > 0; a--) {
  29.     if(argv[a][0] == '-') {
  30.       switch ( argv[a][1] ) {
  31.         // ever wonder why you have to use "break" in switch
  32.         // statements? it's so you can clump more than one case
  33.         // together, like this:
  34.         case 'r' : case 'R' : format |= subdirs; break;
  35.         case 's' : case 'S' : format |= sizes; break;
  36.         case 't' : case 'T' : format |= times; break;
  37.         case 'd' : case 'D' : format |= dates; break;
  38.         case 'a' : case 'A' : format |= attributes; break;
  39.         default :
  40.             fprintf(stderr, "unknown switch: %s\n", argv[a]);
  41.             fprintf(stderr, "%s\n", errmsg);
  42.             exit(1);
  43.       }
  44.     }
  45.   }
  46.   // Notice that the bulk of main() is taken up with the user
  47.   // interface (above), and all the complicated work is hidden in 
  48.   // the classes.  This is as it should be for a good design, and
  49.   // it allows you to use the same user interface for many
  50.   // different programs.
  51.   file_list files(arg);
  52.   if(format & subdirs) files.expand_list();
  53.   files.display_all(format);
  54. }
  55.   
  56.